<?php
/**
 * Author: DefinitlyEvil
 * Created at: 2020/4/16 14:53
 */

namespace App\ServerAPI;


use App\Controller;
use App\Tools\ECSTools;
use App\Tools\Pattern;

class RoomController extends Controller
{
    public static function register(\FastRoute\RouteCollector $r) {
        $r->post('/info', [self::class, 'info']);
        $r->post('/delete', [self::class, 'delete']);
        $r->post('/updatePlayers', [self::class, 'updatePlayers']);
        $r->post('/updateExpireTime', [self::class, 'updateExpireTime']);
        $r->post('/updateIP', [self::class, 'updateIP']);
    }

    public function info($params = []) {
        if(!isset($params['i']) or !is_string($params['i']) or !Pattern::matches('/^i\\-[a-z0-9]+$/', $params['i'])) {
            return ['invalid instance id', false];
        }
        $ecs_instance_id = &$params['i'];
        $stm = $this->getPdo()->prepare('SELECT * FROM `rooms` WHERE `ecs_instance_id`=:i');
        $stm->bindParam(':i', $ecs_instance_id);
        if($stm->execute() === false) return ['database error', false];
        $result = $stm->fetch(\PDO::FETCH_ASSOC);
        if($result == false) return ['room not found', false];
        return [['room' => $result], true];
    }

    public function delete($params = []) {
        if(!isset($params['i']) or !is_string($params['i']) or !Pattern::matches('/^i\\-[a-z0-9]+$/', $params['i'])) {
            return ['invalid instance id', false];
        }
        $ecs_instance_id = &$params['i'];
        $stm = $this->getPdo()->prepare('UPDATE `rooms` SET `expire_time`=0 WHERE `ecs_instance_id`=:i');
        $stm->bindParam(':i', $ecs_instance_id);
        if($stm->execute() === false) trigger_error('failed updating room state to expired');

        ECSTools::initClient();
        ECSTools::delete($ecs_instance_id);
        return true;
    }

    public function updatePlayers($params = []) {
        if(
            !isset($params['i']) or !isset($params['o']) or !isset($params['m']) or
            !Pattern::matches('/^[a-z0-9\-]+$/', $params['i']) or
            !is_int($params['o']) or !is_int($params['m'])
        ) {
            return ['invalid params', false];
        }
        $instance = $params['i'];
        $online = $params['o'];
        $max_online = $params['m'];

        $pdo = $this->getPdo();
        $stm = $pdo->prepare('UPDATE `rooms` SET `players_online`=:o,`players_max`=:m WHERE `ecs_instance_id`=:i');
        $stm->bindParam(':i', $instance, \PDO::PARAM_STR);
        $stm->bindParam(':o', $online, \PDO::PARAM_INT);
        $stm->bindParam(':m', $max_online, \PDO::PARAM_INT);
        if($stm->execute() === false) return ['database error', false];
        return true;
    }

    public function updateExpireTime($params = []) {
        if(!isset($params['i']) or !isset($params['e']) or !is_int($params['e'])) {
            return ['invalid params', false];
        }
        $instance = $params['i'];
        $expire_time = $params['e'];

        $pdo = $this->getPdo();
        $stm = $pdo->prepare('UPDATE `rooms` SET `expire_time`=:ex WHERE `ecs_instance_id`=:i');
        $stm->bindParam(':i', $instance, \PDO::PARAM_STR);
        $stm->bindParam(':ex', $expire_time, \PDO::PARAM_INT);
        if($stm->execute() === false) return ['database error', false];
        return true;
    }

    public function updateIP($params = []) {
        if(!isset($params['i']) or !isset($params['ip']) or !Pattern::matches('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $params['ip'])) {
            return ['invalid params', false];
        }
        $instance = $params['i'];
        $ip = $params['ip'];

        $pdo = $this->getPdo();
        $stm = $pdo->prepare('UPDATE `rooms` SET `connect_address`=:ip WHERE `ecs_instance_id`=:i');
        $stm->bindParam(':i', $instance, \PDO::PARAM_STR);
        $stm->bindParam(':ip', $ip, \PDO::PARAM_INT);
        if($stm->execute() === false) return ['database error', false];
        return true;
    }
}
